import { find, Sprite, SpriteFrame } from 'cc';
import { view } from 'cc';
import { UITransform } from 'cc';
import { _decorator, Component, Label, Node } from 'cc';
import { App } from '../../../App';
import AbstractComponent from '../../../base/AbstractComponent';
import { EventConst } from '../../../common/Common';
import { MultiCommon } from '../../../hall/multiplay/MultiCommon';
import { showLuckyBallRule, showTipBox } from '../../../tools/PopuUp';
import UtilTool from '../../../tools/UtilTool';
import { GameModel } from '../model/GameModel';
import { GameLayer4 } from './GameLayer4';
import { GameModel4 } from './GameModel4';
const { ccclass, property } = _decorator;

// 结算界面
@ccclass('EndLayer4')
export class EndLayer4 extends Component {
    @property(Node)
    successNode : Node = null

    @property(Node)
    failNode : Node = null


    private viewState = null

    start() {
        this.node.getComponent(UITransform).setContentSize(view.getVisibleSize())
    }

    /** 展示界面
     * @param state [1]胜利界面 [2]失败界面
     */
    showView(data, state) {
        this.viewState = state
        UtilTool.nodeActive(state==1?this.successNode:this.failNode,5,0.16)

        this.successNode.active = state==1
        this.failNode.active = state==2

        if (state==1) {
            App.Audio.playOneShot("game/win")
            let coinLabel = find("coinLabel",this.successNode).getComponent(Label)
            coinLabel.string = "+" + data.reward

            // 星币数
            let starCoinLabel = find("starCoinLabel",this.successNode).getComponent(Label)
            let starCoin = find("starCoin",this.successNode)
            starCoinLabel.string = "+" + data.winExperience
            UtilTool.adaptiveLabel([starCoinLabel.node, starCoin], 0, 5)
        }
        else if (state==2) {
            App.Audio.playOneShot("game/fail")

            // 星币数
            let starCoinLabel = find("starCoinLabel",this.failNode).getComponent(Label)
            let starCoin = find("starCoin",this.failNode)
            starCoinLabel.string = "+" + data.loseExperience
            UtilTool.adaptiveLabel([starCoinLabel.node, starCoin], 0, 5)
        }
        else {
            this.node.destroy()
        }
    }

    /** 
     * 再次挑战
      */
    clickAgain() {
        App.Audio.playOneShot("click")
        if (App.Model.gameManage.roomInfoChange) {
            this.node.destroy();
            let gameView = App.Model.gameView as GameModel4
            gameView.gameLogic.exitGame()
            App.Model.gameView.backToMultiRoom(App.Model.gameManage.roomType, {"joinData":App.Model.gameManage.roomInfoChange,"exitType":0})
        }
    }

    /** 
     * 确定
      */
    clickSure() {
        App.Audio.playOneShot("click")
        if (App.Model.gameManage.roomInfoChange) {
            this.node.destroy();
            let gameView = App.Model.gameView as GameModel4
            gameView.gameLogic.exitGame()
            App.Model.gameView.backToMultiRoom(App.Model.gameManage.roomType, {"joinData":App.Model.gameManage.roomInfoChange,"exitType":1})
        }
    }

    onDestroy() {
        
    }

}

